/*
 * ExitUniterruptableThreadsMonitor.java
 *
 * Created on March 12, 2009, 11:05 PM
 */

package net.sf.bluex.boundary;

import net.sf.bluex.components.monitors.ExitThreadMonitor;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import net.sf.bluex.threads.MyThread;
import net.sf.bluex.threads.UninterruptableThread;

/**
 *
 * @author  Blue
 */
public class ExitUniterruptableThreadsMonitor extends javax.swing.JDialog {

    private ExitThreadMonitor etm;
    private static ExitUniterruptableThreadsMonitor eutm;

    private boolean exit;
    
    /** Creates new form ExitUniterruptableThreadsMonitor */
    private ExitUniterruptableThreadsMonitor(java.awt.Frame parent) {
        super(parent, false);
        initComponents();
        
        this.getRootPane().setDefaultButton(cmdCancel);
        
        initListData();

        //start the exit thread monitor
        etm=new ExitThreadMonitor(lstThreads);
        
        setTitle("Exiting BlueX");
        this.setIconImage(net.sf.bluex.controller.UsefulMethods.getImage(net.sf.bluex.controller.FileModule.APP_ICON));
        this.setLocationRelativeTo(parent);
        this.setVisible(true);
    }
    
    private void initListData(){
        java.util.Vector<String> runningThreads=new java.util.Vector<String>();
        
        for(Thread t : MyThread.getRunningThreads(UninterruptableThread.class)){
                runningThreads.add(t.getName());
        }
        
        lstThreads.setListData(runningThreads);
    }
    
    public static boolean showRunningThreads(java.awt.Frame parent){
        if(eutm==null)
            eutm=new ExitUniterruptableThreadsMonitor(parent);
        if(eutm.isVisible())
            eutm.requestFocus();
        else
            eutm.setVisible(true);
        
        return eutm.exit;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstThreads = new javax.swing.JList();
        cmdCancel = new javax.swing.JButton();
        cmdExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Following Uninterruptable Threads are running:");

        jScrollPane1.setViewportView(lstThreads);

        cmdCancel.setText("Cancel");
        cmdCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelActionPerformed(evt);
            }
        });

        cmdExit.setText("Exit BlueX");
        cmdExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cmdExit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdCancel))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
                    .addComponent(jLabel1))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cmdCancel, cmdExit});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdCancel)
                    .addComponent(cmdExit))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdExitActionPerformed
// TODO add your handling code here:
    //with beep
    Toolkit.getDefaultToolkit().beep();
    int choice=JOptionPane.showConfirmDialog(this,
            "WARNING: Killing a running uniterruptable thread can cause" +
            "undersired results including loss of data and object instability." +
            "The thread will not be given the chance to save its state or\n" +
            "data before it is terminated." +
            "Are you sure you want to kill (the) thread(s)?", "Exit Warning", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);

    if(choice==JOptionPane.YES_OPTION){
        exit=true;
        this.dispose();
    }
}//GEN-LAST:event_cmdExitActionPerformed

private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelActionPerformed
// TODO add your handling code here:
    exit=false;
    etm.stop();
    this.dispose();
}//GEN-LAST:event_cmdCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdCancel;
    private javax.swing.JButton cmdExit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstThreads;
    // End of variables declaration//GEN-END:variables

}
